
;define(['../../apis'], function(apis){

    //文档和教程
    //http://pixijs.download/release/docs/index.html
    //https://github.com/Zainking/learningPixi

    class Gamepixi{

        // $com : null,   game.js 组件实例

        //resources: null,  //游戏资源

        constructor() {
            this.app = null;
            this.loader = null;

            this.config = {
                time: 39.3
            }

            this.data = {
                status: 'void', //void load wait play over
            };


            this.init();
        }

        init(){

            var html = document.documentElement;
            var hch  = html.clientHeight;
            var hcw  = html.clientWidth;

            let h = (750/hcw*hch)+18;

            // console.log(hch, hcw, h);

            let app = this.app = new PIXI.Application({
                width: 750,         // default: 800
                height: h<1150?1150:h,  // default: 600 1364 最佳1150 低于1150用1150，高于用屏幕高度
                antialias: true,    // default: false
                transparent: true, // default: false
                resolution: 1       // default: 1
            });
           
            // console.log(app)
            let game = document.querySelector('#game');
            game.innerHTML='';
            game.appendChild(app.view);
            
            app.renderer.view.style.position = "absolute";
            app.renderer.view.style.display = "block";
            // app.renderer.autoDensity = true;
            // app.renderer.resize(window.innerWidth, window.innerHeight);
            window.addEventListener('resize', function(){
                scaleToWindow(app.renderer.view, "top");
                // app.renderer.resize(window.innerWidth, window.innerHeight);
            }, false);
            scaleToWindow(app.renderer.view, "top");

            this.loader = PIXI.Loader.shared;


            this.load().then((res)=>{
                this.wait(res);
            })
						/*
            var sound2 = new Howl({
                src: ['assets/image/bg2.mp3'],
                loop: true
            });
            this.sound2 = sound2;
            document.addEventListener("visibilitychange", function(){
                if(document.hidden){
                    sound2.stop();
                }else{
                    sound2.play();
                }
            });
            */

        }

        onEnd(){
            
        }
        onStart(){

        }
        
        load(){
            
            const loader = this.loader;
            this.data.status = "load";

            return new Promise((resolve, reject)=>{

                if(this.loader && Object.keys(this.loader.resources).length){
                    resolve(this.loader.resources);
                }else{
                    loader.add(`cat`, `assets/image/cat2.jpg`)
                    // loader.add(`ca5t`, `assets/image/cat2.jpg?v=5`)
                    // loader.add(`ca3t`, `assets/image/cat2.jpg?v=2`)
                    // loader.add(`ca4t`, `assets/image/cat2.jpg?v=3`)
                    // loader.add(`ca2t`, `assets/image/cat2.jpg?v=1`)
                    // loader.add(`airship`, `https://img.alicdn.com/imgextra/i3/555657275/O1CN016Sz37V23bzxQGjoXH_!!555657275.png`)
                    // loader.add(`btn`, `https://img.alicdn.com/imgextra/i1/555657275/O1CN01xjdaMJ23bzxSenJYw_!!555657275.png`)
                    // loader.add(`bg0`, `https://img.alicdn.com/imgextra/i2/555657275/O1CN01tiljvY23bzxPl79Ay_!!555657275.png`)
                    // loader.add(`bg1`, `https://img.alicdn.com/imgextra/i2/555657275/O1CN01hMHni723bzxPSYK8J_!!555657275.png`)
                    // loader.add(`bg2`, `https://img.alicdn.com/imgextra/i1/555657275/O1CN01pFYOwy23bzxPSW6uy_!!555657275.png`)
                    // loader.add(`cloud`, `https://img.alicdn.com/imgextra/i1/555657275/O1CN01XHXyM223bzxUFKYU2_!!555657275.png`)
                    // loader.add(`label`, `https://img.alicdn.com/imgextra/i3/555657275/O1CN01FRej5K23bzxR5g5V7_!!555657275.png`)
                    // loader.add(`rule`, `https://img.alicdn.com/imgextra/i4/555657275/O1CN011y3B6023bzxQL8Uod_!!555657275.png`)

                    //loader.add({
                    //    name: 'sprites',
                    //    url: 'assets/image/sprites.json',
                    //    crossOrigin: true
                    //})

                    loader.load((loader, resources) => {
                        console.log("resources:",resources);
                        resolve(resources);
                    });


                    //加载进度
                    const style = new PIXI.TextStyle({
                        align:"center",
                        fontFamily: 'Arial',
                        fontSize: 28
                    });
                    const loadingLabel = new PIXI.Text(`0%`, style);
                    loadingLabel.position.set((this.app.view.width-loadingLabel.width)/2, (this.app.view.height-loadingLabel.height)/2);
                    this.app.stage.addChild(loadingLabel);
                    if(loader.progress == 0){
                        loadingLabel.text = `0%`;
                        loadingLabel.position.set((this.app.view.width-loadingLabel.width)/2, (this.app.view.height-loadingLabel.height)/2);
                    }
                    loader.onProgress.add(() => {
                        // console.log(loader.progress)
                        let progress = parseInt(loader.progress);
                        loadingLabel.text = `${progress}%`;
                        loadingLabel.position.set((this.app.view.width-loadingLabel.width)/2, (this.app.view.height-loadingLabel.height)/2);
                    });


                }
            })

        }
        
        // 等待开始游戏
        wait(resources){

            if(this.data.status == 'wait')return 0;

            this.app.stage.removeChildren();

            if(!resources){
                resources = this.loader.resources;
            }

            setTimeout(()=>{
                this.play();
            })
            
        }
        
        play(resources){

            // this.sound1.stop();
            

            if(this.data.status == 'play')return 0;

            this.app.stage.removeChildren();

            if(!resources){
                resources = this.loader.resources;
            }


            const cat = new PIXI.Sprite(resources.cat.texture);

            cat.width = 250;
            cat.height = 250;
            
            this.app.stage.addChild(cat);
            
            
            //this.app.ticker.add();
        }
        
    }


    return Gamepixi;

});